package com.glc.gao.gavin.rxjavademo;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.glc.gao.gavin.rxjavademo.operator.BufferActivity;
import com.glc.gao.gavin.rxjavademo.operator.ConcatActivity;
import com.glc.gao.gavin.rxjavademo.operator.ConcatMapActivity;
import com.glc.gao.gavin.rxjavademo.operator.CreateActivity;
import com.glc.gao.gavin.rxjavademo.operator.DebounceActivity;
import com.glc.gao.gavin.rxjavademo.operator.DistinctActivity;
import com.glc.gao.gavin.rxjavademo.operator.DoOnNextActivity;
import com.glc.gao.gavin.rxjavademo.operator.FilterActivity;
import com.glc.gao.gavin.rxjavademo.operator.FlatMapActivity;
import com.glc.gao.gavin.rxjavademo.operator.IntervalActivity;
import com.glc.gao.gavin.rxjavademo.operator.LastActivity;
import com.glc.gao.gavin.rxjavademo.operator.MapActivity;
import com.glc.gao.gavin.rxjavademo.operator.MergeActivity;
import com.glc.gao.gavin.rxjavademo.operator.ReduceActivity;
import com.glc.gao.gavin.rxjavademo.operator.ScanActivity;
import com.glc.gao.gavin.rxjavademo.operator.SingleActivity;
import com.glc.gao.gavin.rxjavademo.operator.SkipActivity;
import com.glc.gao.gavin.rxjavademo.operator.TakeActivity;
import com.glc.gao.gavin.rxjavademo.operator.TimerActivity;
import com.glc.gao.gavin.rxjavademo.operator.ZipActivity;

public class MainActivity extends ListActivity {

    private ArrayAdapter<Item> adapter;
    private Item[] items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        items = initActivityData();
        adapter = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, items);
        setListAdapter(adapter);
    }

    private Item[] initActivityData() {
        Item[] items = {
                new Item(CreateActivity.class, getString(R.string.tag_create)),
                new Item(MapActivity.class, getString(R.string.tag_map)),
                new Item(ZipActivity.class, getString(R.string.tag_zip)),
                new Item(ConcatActivity.class, getString(R.string.tag_concat)),
                new Item(FlatMapActivity.class, getString(R.string.tag_flat_map)),
                new Item(ConcatMapActivity.class, getString(R.string.tag_concat_map)),
                new Item(DistinctActivity.class, getString(R.string.tag_distinct)),
                new Item(FilterActivity.class, getString(R.string.tag_filter)),
                new Item(BufferActivity.class, getString(R.string.tag_buffer)),
                new Item(TimerActivity.class, getString(R.string.tag_timer)),
                new Item(IntervalActivity.class, getString(R.string.tag_interval)),
                new Item(SkipActivity.class, getString(R.string.tag_skip)),
                new Item(TakeActivity.class, getString(R.string.tag_take)),
                new Item(DoOnNextActivity.class, getString(R.string.tag_doOnNext)),
                new Item(SingleActivity.class, getString(R.string.tag_single)),
                new Item(DebounceActivity.class, getString(R.string.tag_debounce)),
                new Item(LastActivity.class, getString(R.string.tag_last)),
                new Item(MergeActivity.class, getString(R.string.tag_merge)),
                new Item(ReduceActivity.class, getString(R.string.tag_reduce)),
                new Item(ScanActivity.class, getString(R.string.tag_scan)),
        };
        return items;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        startActivity(items[position]);
    }

    public class Item extends Intent {
        String s;

        public Item(Class<? extends Activity> c, String s) {
            super(MainActivity.this, c);
            this.s = s;
            putExtra("key_title", s);
        }

        @Override
        public String toString() {
            return s;
        }
    }
}
